package com.zzyl.nursing.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.zzyl.common.core.domain.R;
import com.zzyl.nursing.domain.DeviceDto;
import com.zzyl.nursing.vo.DeviceDetailVo;
import com.zzyl.nursing.vo.ProductByProductKeyVo;
import com.zzyl.nursing.vo.ProductVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zzyl.common.annotation.Log;
import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.enums.BusinessType;
import com.zzyl.nursing.domain.Device;
import com.zzyl.nursing.service.IDeviceService;
import com.zzyl.common.utils.poi.ExcelUtil;
import com.zzyl.common.core.page.TableDataInfo;

/**
 * 设备表Controller
 *
 * @author Adam
 * @date 2025-08-01
 */
@RestController
@RequestMapping("/nursing/device")
@Api(tags = "设备表相关接口")
public class DeviceController extends BaseController {
    @Autowired
    private IDeviceService deviceService;

    /**
     * 查询设备表列表
     */
    @PreAuthorize("@ss.hasPermi('nursing:device:list')")
    @GetMapping("/list")
    @ApiOperation(value = "获取设备表列表")
    public TableDataInfo<List<Device>> list(Device device) {
        startPage();
        List<Device> list = deviceService.selectDeviceList(device);
        return getDataTable(list);
    }

    /**
     * 导出设备表列表
     */
    @PreAuthorize("@ss.hasPermi('nursing:device:export')")
    @Log(title = "设备表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation("导出设备表列表")
    public void export(HttpServletResponse response, Device device) {
        List<Device> list = deviceService.selectDeviceList(device);
        ExcelUtil<Device> util = new ExcelUtil<Device>(Device.class);
        util.exportExcel(response, list, "设备表数据");
    }

    /**
     * 获取设备表详细信息
     */
//    @PreAuthorize("@ss.hasPermi('nursing:device:query')")
//    @GetMapping(value = "/{id}")
//    @ApiOperation("获取设备表详细信息")
//    public R<Device> getInfo(@ApiParam(value = "设备表ID", required = true)
//                             @PathVariable("id") Long id) {
//        return R.ok(deviceService.selectDeviceById(id));
//    }

    /**
     * 新增设备表
     */
    @PreAuthorize("@ss.hasPermi('nursing:device:add')")
    @Log(title = "设备表", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("新增设备表")
    public AjaxResult add(@ApiParam(value = "设备表实体", required = true) @RequestBody Device device) {
        return toAjax(deviceService.insertDevice(device));
    }

    /**
     * 修改设备表
     */
    @PreAuthorize("@ss.hasPermi('nursing:device:edit')")
    @Log(title = "设备表", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("修改设备表")
    public AjaxResult edit(@ApiParam(value = "设备表实体", required = true) @RequestBody Device device) {
        return toAjax(deviceService.updateDevice(device));
    }

    /**
     * 删除设备表
     */
//    @PreAuthorize("@ss.hasPermi('nursing:device:remove')")
//    @Log(title = "设备表", businessType = BusinessType.DELETE)
//    @DeleteMapping("/{ids}")
//    @ApiOperation("删除设备表")
//    public AjaxResult remove(@ApiParam(value = "要删除的数据id的数组") @PathVariable Long[] ids) {
//        return toAjax(deviceService.deleteDeviceByIds(ids));
//    }

    /**
     * 同步设备数据
     *
     * @return
     */
    @PostMapping("/syncProductList")
    public AjaxResult SyncProductList() {
        deviceService.SyncProductList();
        return success();
    }

    /**
     * 查询所有产品列表
     *
     * @return
     */
    @GetMapping("/allProduct")
    public AjaxResult AllProduct() {
        List<ProductVo> productVoList = deviceService.AllProduct();
        return success(productVoList);
    }

    /**
     * 注册设备
     *
     * @return
     */
    @PostMapping("/register")
    public AjaxResult register(@RequestBody DeviceDto dto) {
        deviceService.register(dto);
        return success();
    }

    /**
     * 查询设备详细数据
     *
     * @return
     */
    @ApiOperation("获取设备表详细信息")
    @GetMapping("/{iotId}")
    public AjaxResult queryDevice(@PathVariable("iotId") String iotId) {
        DeviceDetailVo deviceDetailVo = deviceService.queryDevice(iotId);
        return success(deviceDetailVo);
    }

    /**
     * 查看设备上报的数据
     *
     * @return
     */
    @GetMapping("/queryServiceProperties/{iotId}")
    public AjaxResult queryServiceProperties(@PathVariable String iotId) {
        List<Map<String, Object>> mapList = deviceService.queryServiceProperties(iotId);
        return success(mapList);
    }

    /**
     * 删除设备
     *
     * @return
     */
    @DeleteMapping("/{iotId}")
    public AjaxResult deleteDevice(@PathVariable String iotId) {
        deviceService.deleteDevice(iotId);
        return success();
    }


    /**
     * 查询产品详情(报警规则模块使用)
     *
     * @return
     */
    @GetMapping("/queryProduct/{productKey}")
    public AjaxResult queryProductByProductKey(@PathVariable String productKey) {
        ProductByProductKeyVo productByProductKeyVo = deviceService.queryProductByProductKey(productKey);
        return success(productByProductKeyVo);
    }

}
